<?php
/**********************************************************************************
 
 	Functions to datamine information from WoW related websites.
	
	Copyright (C) 2010 Benjamin Thomas
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 	Author: Benjamin Thomas
	Email: thomas.ben@gmail.com
 	Created: 06.19.10
 	Revised: 06.20.10
 
 	Calls: 
 
 ***********************************************************************************
 */

//Look up item id
function getItemId($str) {
	echo "getItemId($str) called.<BR>"; //ob_flush();
	set_time_limit(30);
	usleep(300000);
	$str = urlencode($str);
	if (!$xml = file_get_contents("http://www.wowhead.com/item={$str}&xml")) {
		return false;
	}
	$item = XML2ArraY( $xml );
	if ( array_key_exists("item", $item) ) {
		return $item['item']['@attributes']['id'];
	}
	
}

// Look up item information
function getItemData2($id) {
	usleep(1000000);
	set_time_limit(30);
	//ob_flush();
	
	$xml = file_get_contents("http://wow.allakhazam.com/cluster/item-xml.pl?witem={$id}");
	$item = XML2ArraY( $xml );
	if (!$item) {
		print "getItemData({$id}) failed to get XML from http://wow.allakhazam.com/cluster/item-xml.pl?witem={$id}.\n";
		return false;
	}
	if (!array_key_exists('isvendor',$item)) {
		print "Array key 'isvendor' not found. Exiting.\n $xml: ";
		print_r($xml);
		//ob_flush();
		exit();
	}
	
	if ($item['isvendor']) {
		$price = $item['sellprice'];
	} else {
		$price = 0;
	}
	$arr = array( 'price' => $price, 'quality' => $item['quality'], 'icon' => substr($item['icon'],14), 'name' => $item["name1"] );
	unset($item);
	return $arr;
}

function getItemData($id, $debug = false) {
	echo "getItemData($id) called.<BR>";
	//ob_flush();
	set_time_limit(20);
	usleep(300000);
	$xml = file_get_contents("http://www.wowarmory.com/item-info.xml?locale=en_us&i={$id}&rhtml=n");
	//ob_flush();
	if (!$xml) {
		print "getItemData({$id}) failed to get XML from http://www.wowarmory.com/item-info.xml?i={$id}&rhtml=n.\n";
		return false;
	}
	$vendor = XML2Array($xml);
	if ($debug) {
		print_r($vendor);
	}
	
	//Make sure it's a vaid item.
	if (!array_key_exists('itemInfo', $vendor) && !array_key_exists('item', $vendor['itemInfo']) && !array_key_exists('@attributes', $vendor['itemInfo']['item'])) {
		return false;
	}
	$name = $vendor['itemInfo']['item']['@attributes']['name'];
	$icon = $vendor['itemInfo']['item']['@attributes']['icon'] . '.png';
	$quality = $vendor['itemInfo']['item']['@attributes']['quality'];
	$price = 0;
	if (array_key_exists('vendors', $vendor['itemInfo']['item'])) {
		if (!is_array($vendor['itemInfo']['item']['vendors']['creature'])) {
			if (isset($vendor['itemInfo']['item']['cost']['@attributes']['buyPrice'])) {
				$price = $vendor['itemInfo']['item']['cost']['@attributes']['buyPrice'];
			}
		}
	}
	
	$arr = array( 'price' => $price, 'quality' => $quality, 'icon' => $icon, 'name' => $name );
	return $arr;
}

function quality2Color($int) {

}

function XML2Array ( $xml , $recursive = false )
{
    if ( ! $recursive )
    {
        $array = simplexml_load_string ( $xml ) ;
    }
    else
    {
        $array = $xml ;
    }
    
    $newArray = array () ;
    $array = ( array ) $array ;
    foreach ( $array as $key => $value )
    {
        $value = ( array ) $value ;
        if ( isset ( $value [ 0 ] ) )
        {
            $newArray [ $key ] = trim ( $value [ 0 ] ) ;
        }
        else
        {
            $newArray [ $key ] = XML2Array ( $value , true ) ;
        }
    }
    return $newArray ;
}






?>